package cn.feizhu.aimarket.ai.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

public class JsonToObjectValidator {
    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * 验证 JSON 字符串是否能转换为指定类型的对象
     * @param json JSON 字符串
     * @param targetClass 目标类类型
     * @return 转换成功返回 true，否则返回 false
     */
    public static <T> boolean canConvertToObject(String json, Class<T> targetClass) {
        try {
            mapper.readValue(json, targetClass);
            return true;
        } catch (JsonProcessingException e) {
            return false;
        }
    }
}
